<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <style>
    </style>
</head>
<style>
    .home:before {
        content: url(../image/home.png);
    }

    .refresh:before {
        content: url(../image/refresh.png);
    }

    .company:before {
        content: url(../image/company.png);
    }

    .pa-title {
        font-size: 17px;
        color: black;
        font-weight: bold;
    }

    .pa-title-content {
        font-size: 17px;
        color: black;
    }

    body {
        background-color: #D9D8DB;
    }


    .pa_xg_defect_t {
        background-color: #254060;
        height: 385px;
        color: white;
        overflow: auto;
    }

    .pa_xg_defect {
        margin-top: 5px;
        height: 40px;
        line-height: 40px;
        margin-left: 2%;
        text-align: center;
        color: white;
    }

    .pa_xg_defect input {
        height: 40px;
        min-height: 40px;
    }

    .pa_xg_defect_but {
        height: 50px;
        line-height: 50px;
        margin-left: 50px;
        float: left;
        width: 130px;
        font-size: 27px;
        color: white;
        text-align: center;
    }

    p {
        font-size: 17px;
        color: black;
        font-weight: bold;
        text-align: center;
    }

    table {
        width: 100%;
        table-layout: fixed;
        word-wrap: break-word;
    }

    td {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }

    td input {
        text-align: center;
    }

    .hide {
        display: none;
    }

    .c1 {
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, .5);
        z-index: 2;
    }

    .c2 {
        width: 90%;
        height: 50%;
        left: 20%;
        top: 30%;
        background-color: white;
        position: fixed;
        /* width: 400px;
            height: 300px; */
        /* top: 50%;
            left: 50%; */
        z-index: 3;
        margin-top: -150px;
        margin-left: -200px;
        padding-top: 2%;
        padding-left: 2%;
        padding-right: 10px;
    }
</style>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #D9D8DB">
        <div style="width: 10%;height: 60px;float: left;padding-top: 6px;">
            <a class="aui-pull-left aui-btn ">
                <span class="aui-iconfont company"></span>
            </a>
        </div>
        <div style="width: 10%;height: 60px;float: left;">
            <div style="text-align: left;">
                <span class="pa-title">富晟久泰</span>
            </div>
        </div>
        <div style="width: 70%;height: 60px;float: left;">
            <div style="width:20%; float: left; text-align: left;">
                <span class="pa-title-content" id="locationName"></span>
            </div>
            <div style="width:25%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCenterName"></span>
            </div>
            <div style="width:15%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCellName"></span>
            </div>
            <div style="width:40%; float: left; text-align: left;">
                <span class="pa-title-content" id="nowDate"></span>
            </div>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
        </div>
        <!-- <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn" onclick="closeWin()">
                <span class="aui-iconfont home"></span>
            </a>
        </div> -->


    </header>
    <div class="aui-content">
        <div class="aui-flex-col aui-flex-center" style="height: 30px;">
            <div class="aui-flex-item-12">
                <p>下挂_录入不合格数</p>
            </div>
        </div>

        <div class="aui-flex-col aui-flex-center">
            <!-- <div class="aui-flex-item-5">
                <div class="aui-list-item-inner" style="border: 0;margin-left: 10px;margin-top: 5px;">
                    <div class="aui-list-item-label" style="font-size: 25px;float: left;">
                        <span>不合格数：</span>
                        <span id="unQualifyQty"></span>
                    </div>
                </div>
            </div>
            <div class="aui-flex-item-4">
                <div class="aui-list-item-inner" style="border: 0;margin-left: 10px;margin-top: 5px;">
                    <div class="aui-list-item-label" style="font-size: 25px;float: left;">
                        <span>当前总数：</span>
                        <span id="unQualifyQtyNow"></span>
                    </div>
                </div>
            </div> -->
            <div class="aui-flex-item-3">
                <!-- <div class="pa_xg_defect_but" style="background-color: #F59A23;" onclick="split()">
                    拆分
                </div> -->

                <!-- <div class="pa_xg_defect_but" style="background-color: #528302;" onclick="save()">
                    提交
                </div> -->

                <!-- <div class="pa_xg_defect_but" style="background-color: #D9001B;" onclick="cancle()">
                    关闭
                </div> -->
            </div>

        </div>
        <div class="aui-flex-col aui-flex-center" style="margin-top: 1%;">
            <div class="aui-flex-item-12 pa_xg">
                <!-- <div class="aui-flex-item-5">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                            飞&nbsp巴&nbsp号：</div>
                        <div class="aui-list-item-input" style="background-color: white;float: left;width: 250px;">
                            <input type="text" id="fbCode" class="aui-input" onclick="getIdSelector();" />
                        </div>
                    </div>
                </div> -->
                <div class="aui-flex-item-5">
                    <div class="aui-flex-col">
                        <div class="aui-list-item-inner" style="border: 0;">
                            <div class="pa_xg_defect_but"
                                style="background-color: #528302;margin-left: 5px;border-radius: 10px;"
                                onclick="selectOrder()">
                                选择工单
                            </div>
                        </div>
                    </div>
                </div>
                <div class="aui-flex-item-2" style="float: right;">
                    <div class="aui-flex-col">
                        <div class="aui-list-item-inner" style="border: 0;">
                            <div class="pa_xg_defect_but"
                                style="background-color: #528302;margin-left: 5px;border-radius: 10px;"
                                onclick="save()">
                                提交
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="aui-flex-item-12 pa_xg">
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                            <span>飞巴号：</span>
                            <span id="fbCode"></span>
                        </div>
                    </div>
                </div>
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                            <span>工单号：</span>
                            <span id="work_order_code"></span>
                        </div>
                    </div>
                </div>
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">

                            <span>规格：</span>
                            <span id="drawing"></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="aui-flex-item-12 pa_xg">
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">

                            <span>物料号：</span>
                            <span id="produCode"></span>
                        </div>
                    </div>
                </div>
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">

                            <span>物料名：</span>
                            <span id="produName"></span>
                        </div>
                    </div>
                </div>
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">

                            <span>不合格数：</span>
                            <span id="unQualifyQty"></span>
                        </div>
                    </div>
                </div>

            </div>
            <div class="aui-flex-item-12 pa_xg">
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">

                            <span>上挂数量：</span>
                            <span id="hangupQty"></span>
                        </div>
                    </div>
                </div>
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">

                            <span>下挂数量：</span>
                            <span id="hangdownQty"></span>
                        </div>
                    </div>
                </div>

                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">

                            <span>已录入缺陷数：</span>
                            <span id="unQualifyQtyNow"></span>
                        </div>
                    </div>
                </div>
                <div class="aui-flex-item-4">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                        </div>
                        <div class="aui-list-item-input" style="background-color: white;float: left;width: 250px;">
                            <!-- <input type="text" id="hangdownQty" class="aui-input" onclick="getIdSelector();" /> -->
                        </div>
                    </div>
                </div>
            </div>
            <div class="aui-flex-item-12">
                <div class="aui-flex-col pa_xg_defect_t">
                    <div class="aui-flex-item-12">
                        <div style="height: 100%;width: 100%;">
                            <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                                <thead>
                                    <tr style="text-align: center;color: white;font-size: 16px;">
                                        <td algin='center' style="width:4%"></td>
                                        <td algin='center' style="width:6%">序号</td>
                                        <!-- <td algin='center' style="width:15%">容器码</td>
                                        <td algin='center' style="width:14%">物料号</td>
                                        <td algin='center' style="width:15%">物料名</td>
                                        <td algin='center' style="width:10%">批次号</td> -->
                                        <td algin='center' style="width:13%">缺陷类型</td>
                                        <td algin='center' style="width:13%">缺陷名称</td>
                                        <td algin='center' style="width:10%;white-space:pre-wrap">不合格数</td>

                                    </tr>
                                </thead>
                                <tbody id="hangDownDefectDataHtml">

                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="shade" class="c1 hide"></div>
        <div id="modal" class="c2 hide">
            <p style="margin-left: 10px;font-size: 1rem;font-weight: 500;color: #212121;text-align: center;">选择工单</p>
            <div style="height: 100%;width: 100%;">
                <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                    <thead>
                        <tr style="text-align: center;color: #333;font-size: 18px;">
                            <td algin='center' style="width:5%">序号</td>
                            <td algin='center' style="width:12%">工单号</td>
                            <!-- <td algin='center' style="width:10%">上挂容器码</td> -->
                            <!-- <td algin='center' style="width:10%">下挂物料号</td> -->
                            <td algin='center' style="width:12%">规格</td>
                            <td algin='center' style="width:34%">下挂物料名</td>
                            <td algin='center' style="width:8%">批次号</td>
                            <td algin='center' style="width:6%">上挂数</td>
                            <td algin='center' style="width:6%">下挂数</td>
                            <td algin='center' style="width:7%">不合格数</td>
                        </tr>
                    </thead>
                    <tbody id="hangUpRecordDataHtml">
                        <!-- <tr style='text-align: center;color: #333;font-size: 14px;height:30px;'>
                            <td name='index'>1</td><td name='workOrderCode'>GD-20231226-0001</td>
                            <td name='drawing'>4KD 853  054</td>
                            <td name='sitmName'>C8 PA 尾管装饰件右</td>
                            <td name='lotCode'>20231226</td>
                            <td name='hangUpQty'>16</td>
                            <td name='hangDownQty'>16</td>
                            <td name='unQualifyQty'>16</td>
                        </tr>
                        <tr style='text-align: center;color: #333;font-size: 14px;height:30px;'>
                            <td name='index'>2</td>
                            <td name='workOrderCode'>GD-20240217-0001</td>
                            <td name='drawing'>4KD 853  053</td>
                            <td name='sitmName'>C8 PA 尾管装饰件左</td>
                            <td name='lotCode'>20231226</td>
                            <td name='hangUpQty'>5</td>
                            <td name='hangDownQty'>5</td>
                            <td name='unQualifyQty'>5</td>
                        </tr> -->
                    </tbody>
                </table>
            </div>

        </div>

    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";
    var tableList = [];
    var chkItemTypeHtml = "";
    var workOrderList = [];

    //初始化
    apiready = function () {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //产线工位信息
        document.getElementById("locationName").innerHTML = api.pageParam.locationName;
        document.getElementById("workCenterName").innerHTML = api.pageParam.workCenterName;
        document.getElementById("workCellName").innerHTML = api.pageParam.workCellName;
        document.getElementById("nowDate").innerHTML = getTimeCommon();
        document.getElementById("fbCode").innerHTML = api.pageParam.fbCode
        workOrderList = api.pageParam.workOrderList
        //当前时间刷新
        window.setInterval("getTime()", 1000);

        hideselectWin();
        if(workOrderList.length>0){
               document.getElementById("work_order_code").innerHTML = workOrderList.workOrderCode;//工单号
        document.getElementById("drawing").innerHTML = workOrderList.drawing;//规格
        document.getElementById("produCode").innerHTML = workOrderList.ext01;//物料号
        document.getElementById("produName").innerHTML = workOrderList.ext02;//物料名
        document.getElementById("hangupQty").innerHTML = workOrderList.hangUpQty;//上挂数
        document.getElementById("hangdownQty").innerHTML = workOrderList.hangDownQty;//物料名
        }
     

        //刷新列表数据
        doSearchLineItem(workOrderList.workOrderCode);




        //查询缺陷类别下拉框
        // doSearchCombox();

        //查询该产线对应的所有缺陷,和总的不合格数
        // doSearchLineItem();
    }
    //选择工单
    function selectOrder() {
        // 查询当前飞巴下已下挂有不合格但是未录入缺陷的上挂下挂记录按工单汇总
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyHangDownNoChkItemData";
        var data = {
            values: {
                fbCode: api.pageParam.fbCode,
                un, un
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                submitBut = false;
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    list = eval(myobj.list);
                    list2 = eval(myobj.list2);
                    var html = "";

                    console.log('list55:' + JSON.stringify(myobj));

                    if (list.length == 0) {

                        //清空表单
                        clearForm();
                        hideselectWin();
                        api.toast({
                            msg: "飞巴号【" + api.pageParam.fbCode + "】没有上挂记录!",
                            duration: 3000
                        });
                        return;
                    }

                    if (list != null && list.length > 0) {
                        showselectWin();
                        // document.getElementById("workOrderCode").innerHTML = list[0].workOrderCode;
                        // document.getElementById("hangUpQty").innerHTML = myobj.hangUpQty;
                        // document.getElementById("mrlCode").innerHTML = list[0].ext01;
                        // document.getElementById("mrlName").innerHTML = list[0].ext02;

                        var html = "";
                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            var hangUpQtyId = "hangUpQty_" + i;
                            var hangDownQtyId = "hangDownQty_" + i;
                            var unQualifyQtyId = "unQualifyQty_" + i;
                            var refloatQtyId = "refloatQty_" + i;
                            var fbCodeId = "fbCode_" + i;



                            html += "<tr style='text-align: center;color: #333;font-size: 14px;height:30px;' onclick='backOrderData(\"" + list[i].workOrderCode + "\",\""
                                + list[i].drawing + "\",\"" + list[i].ext02 + "\",\"" + list[i].ext03 + "\",\"" + list[i].hangUpQty + "\",\"" + list[i].hangDownQty + "\",\"" + list[i].unQualifyQty + "\",\"" + list[i].ext01 + "\" );' >";
                            html += "<td name='index'>" + (i + 1) + "</td>";
                            html += "<td name='workOrderCode'>" + list[i].workOrderCode + "</td>";
                            // html += "<td name='ctBarCode'>" + list[i].ctBarCode + "</td>";
                            /* html += "<td name='sitmCode'>" + list[i].sitmCode + "</td>";
                            html += "<td name='sitmName'>" + list[i].sitmName + "</td>";
                            html += "<td name='lotCode'>" + list[i].lotCode + "</td>"; */
                            // html += "<td name='sitmCode'>" + list[i].ext01 + "</td>";
                            html += "<td name='drawing'>" + list[i].drawing + "</td>";
                            html += "<td name='sitmName'>" + list[i].ext02 + "</td>";
                            html += "<td name='lotCode'>" + list[i].ext03 + "</td>";
                            html += "<td name='hangUpQty'>" + list[i].hangUpQty + "</td>";
                            html += "<td name='hangDownQty'>" + list[i].hangDownQty + "</td>";
                            html += "<td name='unQualifyQty'>" + list[i].unQualifyQty + "</td>";
                            html += "</tr>";
                        }

                        console.log('html44:' + html);
                        $api.html($api.byId("hangUpRecordDataHtml"), html);

                    }

                } else {
                    //清空表单
                    clearForm();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!");
            }
        });
    }



    function backOrderData(workOrderCode, drawing, ext02, ext03, hangUpQty, hangDownQty, unQualifyQty, ext01) {
        hideselectWin();
        document.getElementById("work_order_code").innerHTML = workOrderCode;//工单号
        document.getElementById("drawing").innerHTML = drawing;//规格
        document.getElementById("produCode").innerHTML = ext01;//物料号
        document.getElementById("produName").innerHTML = ext02;//物料名
        // document.getElementById("unQualifyQty").innerHTML = unQualifyQty;//不合格数
        document.getElementById("hangupQty").innerHTML = hangUpQty;//上挂数
        document.getElementById("hangdownQty").innerHTML = hangDownQty;//物料名

        //刷新列表数据
        doSearchLineItem(workOrderCode);
    }

    function showselectWin() {
        document.getElementById('modal').classList.remove('hide');
        document.getElementById('shade').classList.remove('hide');
    }

    function hideselectWin() {
        document.getElementById('modal').classList.add('hide');
        document.getElementById('shade').classList.add('hide');
    }

    //时间刷新
    function getTime() {
        document.getElementById("nowDate").innerHTML = getTimeCommon();
    }

    function doSearchLineItem(workOrderCode) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getItemListByLine";
        var data = {
            values: {
                workCenterCode: api.pageParam.workCenterCode,
                workOrderCode: workOrderCode,
                fbCode: api.pageParam.fbCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    document.getElementById("unQualifyQty").innerHTML = myobj.unQualifyQty;
                    var list = eval(myobj.list);
                    var html = "";

                    var chkItemType = "chkItemType";
                    var chkItem = "chkItem";

                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        var chkItemType = "chkItemType_" + i;
                        var chkItem = "chkItem_" + i;
                        var unQualifyQtyId = "unQualifyQty_" + i;
                        list[i].unQualifyQty = 0;
                        html += "<tr style='text-align: center;color: white;font-size: 14px;height:30px;'>";
                        html += "<td><input name='sub' type='checkbox' /></td>";
                        html += "<td name='index'>" + (i + 1) + "</td>";
                        // html += "<td name='ctBarCode'>" + list[i].ctBarCode + "</td>";
                        // html += "<td name='sitmCode'>" + list[i].sitmCode + "</td>";
                        // html += "<td name='sitmName'>" + list[i].sitmName + "</td>";
                        // html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                        html += "<td name='chkItemType' id = \"" + chkItemType + "\">" + list[i].chkItemTypeName + "</td>";
                        html += "<td name='chkItem' id = \"" + chkItem + "\">" + list[i].chkItemName + "</td>";
                        html += "<td name='unQualifyQty'><input type='number' id=\"" + unQualifyQtyId + "\" style='color:white; font-size: 14px; height:30px; min-height:30px;' value=\"" + list[i].unQualifyQty + "\" onblur='changeNum(\"" + unQualifyQtyId + "\",\"" + list[i].chkItem + "\" );' /></td>";


                        html += "</tr>";
                    }
                    $api.html($api.byId("hangDownDefectDataHtml"), html);
                    tableList = list;
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }
    // 不合格数改变事件
    function changeNum(unQualifyQtyId, chkItemId) {
        var qty = document.getElementById(unQualifyQtyId).value;
        var qtyALL = 0;
        var qtyN = 0
        for (var i = 0; i < tableList.length; i++) {
            if (tableList[i].chkItem == chkItemId) {
                tableList[i].unQualifyQty = qty;
            }
            qtyN = (tableList[i].unQualifyQty == undefined ? 0 : tableList[i].unQualifyQty);
            // if(Number(qtyN))
            if (!isNaN(Number(qtyN))) {
                qtyALL = qtyALL + Number(qtyN);
            } else {
                api.toast({
                    msg: "请输入数值型不合格数",
                    duration: 3000
                });
            }

        }
        document.getElementById("unQualifyQtyNow").innerHTML = qtyALL;
    }

    //缺陷类别下拉框
    // function doSearchCombox() {
    //     var un = localStorage.getItem('un');
    //     var serverUrl = localStorage.getItem('serverUrl');
    //     var url = serverUrl + "/app/getUtilComboxList";
    //     var data = {
    //         values: {
    //             tablename: "qc_bd_chk_item_type",
    //             column_name: "name",
    //             column_value: "sysid",
    //             condition: "is_delete=0",
    //             un: un,
    //         }
    //     };
    //     var params = new Object();
    //     params.url = url;
    //     params.dataType = 'json';
    //     params.method = 'post';

    //     ajaxForData(params, data, false, function (ret, err) {
    //         if (ret) {
    //             var data = JSON.stringify(ret);
    //             var myobj = eval("(" + data + ")");

    //             //成功
    //             if (myobj.code == 0) {
    //                 var list = eval(myobj.list);
    //                 var html = "";
    //                 //遍历
    //                 for (var i = 0; i < list.length; i++) {
    //                     html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
    //                 }

    //                 chkItemTypeHtml = html;
    //                 //查询下挂记录
    //                 doSearch();
    //             } else {
    //                 api.toast({
    //                     msg: myobj.message,
    //                     duration: 3000
    //                 });
    //             }
    //         } else {
    //             alert("网络连接超时!")
    //         }
    //     });
    // }

    function doSearchCombox() {
        chkItemId = "";
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getFlawComboxListByLine";
        var data = {
            values: {
                workCenterCode: api.pageParam.workCenterCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }
                    chkItemTypeHtml = html;
                    //查询下挂记录
                    doSearch();
                    //
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询下挂-不合格记录
    function doSearch() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyHangUpData";
        var data = {
            values: {
                fbCode: api.pageParam.fbCode,
                workOrderCode: api.pageParam.workOrderCode,
                state: 1, //状态：0-上挂，1-下挂
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";

                    if (list != null && list.length > 0) {
                        document.getElementById("unQualifyQty").innerHTML = myobj.unQualifyQty;

                        var html = "";

                        var chkItemType = "chkItemType";
                        var chkItem = "chkItem";

                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            var chkItemType = "chkItemType_" + i;
                            var chkItem = "chkItem_" + i;
                            var unQualifyQtyId = "unQualifyQty_" + i;

                            html += "<tr style='text-align: center;color: white;font-size: 14px;height:30px;'>";
                            html += "<td><input name='sub' type='checkbox' /></td>";
                            html += "<td name='index'>" + (i + 1) + "</td>";
                            html += "<td name='ctBarCode'>" + list[i].ctBarCode + "</td>";
                            html += "<td name='sitmCode'>" + list[i].sitmCode + "</td>";
                            html += "<td name='sitmName'>" + list[i].sitmName + "</td>";
                            html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                            html += "<td name='unQualifyQty'><span id=\"" + unQualifyQtyId + "\" >" + list[i].unQualifyQty + "</span></td>";
                            html += "<td name='chkItemType'><select id = \"" + chkItemType + "\" style='font-size:14px;color:white;height:30px;min-height:30px;line-height:30px;text-align:center;' onchange='onChange(\"" + chkItemType + "\" ,\"" + chkItem + "\")'>" + chkItemTypeHtml + "</select></td>";
                            html += "<td name='chkItem'><select style='font-size:14px;color:white;height:30px;line-height:30px;min-height:30px;text-align:center;' id = \"" + chkItem + "\" style=''></select></td>";
                            html += "</tr>";
                        }

                        $api.html($api.byId("hangDownDefectDataHtml"), html);
                        tableList = list;
                    } else {

                        //清空表单
                        clearForm();
                    }

                } else {
                    //清空表单
                    clearForm();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!");
            }
        });
    }

    //缺陷类别值改变
    function onChange(chkItemType, chkItem) {
        var chkItemTypeId = $("#" + chkItemType).val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getUtilComboxList";
        var data = {
            values: {
                tablename: "qc_qual_chk_item",
                column_name: "name",
                column_value: "sysid",
                condition: "chk_item_type_gid= '" + chkItemTypeId + "'",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                    }

                    $api.html($api.byId(chkItem), html);
                } else {
                    $api.html($api.byId(chkItem), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //清空表单
    function clearForm() {
        document.getElementById("unQualifyQty").innerHTML = "0";
        $api.html($api.byId("hangDownDefectDataHtml"), "");
        tableList = [];
    }

    //拆分
    function split() {
        //选中数据
        var check = $("input[type='checkbox']:checked");
        var count = 0;
        var unQualifyQty = 0;
        var index = 0;
        var row;
        check.each(function () { //遍历
            row = $(this).parent("td").parent("tr");
            index = row.find("[name='index']").html();
            count++;
        });

        if (count != 1) {
            api.toast({
                msg: '请选择一条表格数据！',
                duration: 3000
            });
            return;
        }
        unQualifyQty = $("#unQualifyQty_" + (index - 1)).html();
        if (parseInt(unQualifyQty) == 1) {
            api.toast({
                msg: '不合格数量为【1】，不允许拆分！',
                duration: 3000
            });
            return;
        }

        var dialogBox = api.require('dialogBox');
        dialogBox.input({
            keyboardType: 'default',
            texts: {
                title: "拆分",
                placeholder: '录入数量',
                leftBtnTitle: '取消',
                rightBtnTitle: '确定'
            },
            styles: {
                bg: '#fff',
                corner: 2,
                w: 300,
                h: 240,
                title: {
                    h: 60,
                    alignment: 'center',
                    size: 14,
                    color: '#000',
                    marginT: 30,
                },
                input: {
                    h: 60,
                    y: 30,
                    marginT: 15,
                    marginLeft: 10,
                    marginRight: 10,
                    textSize: 20,
                    textColor: '#000',
                    corner: 5,                  //（可选项）数字类型；对输入框圆角大小；默认：0

                },
                dividingLine: {
                    width: 0.5,
                    color: '#696969'
                },
                left: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40,                //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60,                   //(可选项) 数字类型；左边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10,            //(可选项) 数字类型；左边按离底部的边距；默认：0
                    corner: 5,              //(可选项) 数字类型；左边按钮的圆角；默认：0
                    widhtBorder: 1,         //(可选项) 数字类型；左边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；左边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；左边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；左边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23'   //(可选项) 数字类型；左边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                },
                right: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40,                //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60,                   //(可选项) 数字类型；右边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10,            //(可选项) 数字类型；右边按离底部的边距；默认：0
                    corner: 5,              //(可选项) 数字类型；右边按钮的圆角；默认：0
                    widhtBorder: 1,         //(可选项) 数字类型；右边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23'   //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                }
            }
        }, function (ret) {
            //数量
            var splitQty = ret.text;

            //取消
            if (ret.eventType == 'left') {
                dialogBox.close({
                    dialogName: 'input'
                });

                //确定
            } else {

                //卡号必输校验
                if (splitQty == null || splitQty == "" || splitQty == undefined) {
                    api.toast({
                        msg: "请录入数量!",
                        duration: 3000
                    });
                } else if (parseInt(splitQty) <= 0) {

                    api.toast({
                        msg: "数量应大于【0】!",
                        duration: 3000
                    });
                } else if (parseInt(splitQty) >= parseInt(unQualifyQty)) {
                    api.toast({
                        msg: "数量应小于【" + unQualifyQty + "】!",
                        duration: 3000
                    });
                } else {
                    dialogBox.close({
                        dialogName: 'input'
                    });
                    //拆分
                    addRow(unQualifyQty, splitQty, index);
                }
            }
        });
    }

    //表格新增行
    function addRow(unQualifyQty, splitQty, index) {
        $("#unQualifyQty_" + (index - 1)).html(parseInt(unQualifyQty) - parseInt(splitQty));
        var unQualifyQtyId = "unQualifyQty_" + tableList.length;
        var chkItemType = "chkItemType_" + tableList.length;
        var chkItem = "chkItem_" + tableList.length;
        var map = tableList[index - 1];
        map.unQualifyQty = splitQty;
        var html = "";
        html += "<tr style='text-align: center;color: white;font-size: 14px;height:30px;'>";
        html += "<td><input name='sub' type='checkbox' /></td>";
        html += "<td name='index'>" + (tableList.length + 1) + "</td>";
        html += "<td name='ctBarCode'>" + map.ctBarCode + "</td>";
        html += "<td name='sitmCode'>" + map.sitmCode + "</td>";
        html += "<td name='sitmName'>" + map.sitmName + "</td>";
        html += "<td name='lotCode'>" + map.lotCode + "</td>";
        html += "<td name='unQualifyQty'><span id=\"" + unQualifyQtyId + "\" >" + splitQty + "</span></td>";
        html += "<td name='chkItemType'><select id = \"" + chkItemType + "\" style='font-size:14px;color:white;height:30px;min-height:30px;line-height:30px;text-align:center;' onchange='onChange(\"" + chkItemType + "\" ,\"" + chkItem + "\")'>" + chkItemTypeHtml + "</select></td>";
        html += "<td name='chkItem'><select style='font-size:14px;color:white;height:30px;line-height:30px;min-height:30px;text-align:center;' id = \"" + chkItem + "\" style=''></select></td>";
        html += "</tr>";
        $api.append($api.byId("hangDownDefectDataHtml"), html);
        tableList.push(map);
    }

    //保存
    function save() {
        var chkItemType;
        var chkItem;
        var unQualifyQty;
        var josnList = [];
        if (tableList == null || tableList.length == 0) {
            api.toast({
                msg: "没有需要保存的数据！",
                duration: 3000
            });
            return;
        }
        var unQualifyQty = document.getElementById("unQualifyQty").innerHTML;
        var unQualifyQtyNow = document.getElementById("unQualifyQtyNow").innerHTML;
        var workOrderCode = document.getElementById("work_order_code").innerHTML;//工单号
        if (Number(unQualifyQty) != Number(unQualifyQtyNow)) {
            api.toast({
                msg: "当前提交总数不等于不合格数，无法提交！",
                duration: 3000
            });
            return;
        }

        for (var i = 0; i < tableList.length; i++) {
            if (Number(tableList[i].unQualifyQty) > 0) {
                josnList.push(tableList[i]);
            }

        }


        //遍历
        // for (var i = 0; i < tableList.length; i++) {
        //     chkItemType = $("#chkItemType_" + i).val();
        //     //缺陷列表非空校验
        //     if (chkItemType == null || chkItemType == "" || chkItemType == undefined) {
        //         api.toast({
        //             msg: "第【" + (i + 1) + "】行缺陷类别不能为空！",
        //             duration: 3000
        //         });
        //         return;
        //     }
        //     chkItem = $("#chkItem_" + i).val();
        //     //缺陷名称非空校验
        //     if (chkItem == null || chkItem == "" || chkItem == undefined) {
        //         api.toast({
        //             msg: "第【" + (i + 1) + "】行缺陷名称不能为空！",
        //             duration: 3000
        //         });
        //         return;
        //     }
        //     unQualifyQty = $("#unQualifyQty_" + i).html();
        //     josnList.push({
        //         ctBarCode: tableList[i].ctBarCode,
        //         sitmCode: tableList[i].sitmCode,
        //         sitmName: tableList[i].sitmName,
        //         lotCode: tableList[i].lotCode,
        //         unQualifyQty: unQualifyQty,
        //         chkItemType: chkItemType,
        //         chkItem: chkItem
        //     });
        // }
        api.confirm({
            title: '确认',
            msg: "是否确认提交缺陷，提交后将不可更改？",
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                var un = localStorage.getItem('un');
                var serverUrl = localStorage.getItem('serverUrl');
                var url = serverUrl + "/app/getAssemblyHangDownQxSave";
                var data = {
                    values: {
                        josnList: JSON.stringify(josnList),
                        clientCode: equipUniqueId,
                        fbCode: api.pageParam.fbCode,
                        workOrderCode: workOrderCode,
                        unQualifyQtyNow: unQualifyQtyNow,
                        un: un,
                    }
                };

                var params = new Object();
                params.url = url;
                params.dataType = 'json';
                params.method = 'post';

                api.showProgress({
                    title: '保存中...',
                });

                ajaxForData(params, data, false, function (ret, err) {
                    if (ret) {
                        api.hideProgress();
                        var data = JSON.stringify(ret);
                        var myobj = eval("(" + data + ")");

                        //成功
                        if (myobj.code == 0) {

                            api.toast({
                                msg: "保存成功!",
                                duration: 3000
                            });



                            //1、清空页面
                            //2、// 判断当前飞巴下是否还有已下挂，不合格数大于0但是缺陷录入标识为N的数据，有则清空并停留在当前页面，让用户继续选择工单，否则清空页面并返回首页
                            var url = serverUrl + "/app/getAssemblyHangDownNoChkItemData";
                            var data = {
                                values: {
                                    fbCode: api.pageParam.fbCode,
                                    un, un
                                }
                            };
                            var params = new Object();
                            params.url = url;
                            params.dataType = 'json';
                            params.method = 'post';
                            ajaxForData(params, data, false, function (ret, err) {
                                if (ret) {
                                    submitBut = false;
                                    var data = JSON.stringify(ret);
                                    var myobj = eval("(" + data + ")");
                                    var listLength = 0;

                                    //成功
                                    if (myobj.code == 0) {
                                        list = eval(myobj.list);
                                        listLength = list.length;

                                    }
                                } else {
                                    alert("网络连接超时!");
                                }
                                if (listLength == 0) {
                                    //返回上一级
                                    api.closeToWin({ name: api.pageParam.rootName });
                                    api.execScript({
                                        name: api.pageParam.rootName,
                                        script: 'refresh()'
                                    });
                                    api.execScript({
                                        name: api.pageParam.name,
                                        script: 'refresh()'
                                    });
                                } else {
                                    //清空表单
                                    clearForm();
                                    document.getElementById("work_order_code").innerHTML = "";//工单号
                                    document.getElementById("drawing").innerHTML = "";//规格
                                    document.getElementById("produCode").innerHTML = "";//物料号
                                    document.getElementById("produName").innerHTML = "";//物料名
                                    document.getElementById("hangupQty").innerHTML = "";//上挂数
                                    document.getElementById("hangdownQty").innerHTML = "";//下挂数
                                    document.getElementById("unQualifyQty").innerHTML = "";//不合格数
                                    document.getElementById("unQualifyQtyNow").innerHTML = "";//已录入缺陷数
                                }
                            });


                        } else {
                            api.toast({
                                msg: myobj.message,
                                duration: 3000
                            });
                        }
                    } else {
                        api.hideProgress();
                        alert("网络连接超时!");
                    }
                });
            }
        });

    }

    //取消按钮
    function cancle() {
        api.confirm({
            title: '确认',
            msg: "是否确认关闭当前页面，关闭后将无法提交缺陷信息？",
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                api.closeToWin({ name: api.pageParam.rootName });
                api.execScript({
                    name: api.pageParam.rootName,
                    script: 'refresh()'
                });
                api.execScript({
                    name: api.pageParam.name,
                    script: 'refresh()'
                });
            }
        })

    }

    //返回按钮
    function closeWin() {
        api.confirm({
            title: '确认',
            msg: "是否确认返回首页，离开后将无法提交缺陷信息？",
            buttons: ['确定', '取消']
        }, function (ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                api.closeToWin({ name: api.pageParam.rootName });
                api.execScript({
                    name: api.pageParam.rootName,
                    script: 'refresh()'
                });
            }
        })

    }
</script>

</html>